<?
class Share_Cat extends Common{
    var $table = 'categories_multi';
    var $id_field = 'cat_id';
    var $name_field = 'cat_name';
    var $page;
    var $result = array();
    public function __construct($id = 0,$page = 1){
        parent::__construct($id);
        $this->page = $page;
        $this->result = db_first('SELECT cat_id,cat_name
                                  FROM categories_multi
                                  WHERE cat_active = 1 AND cat_type = "share" AND cat_id = "'.$this->id.'"
                                  LIMIT 1');
        if($this->result) {   
            $this->name = $this->result['cat_name'];
            $this->description = 'Các bài viết chia sẻ về '.$this->result['cat_name'];
            $this->url = generate_share_cat_url($this->result);
        }
        //fix cho danh mục chia sẻ chung
        else {
            $this->name = 'Chia sẻ kinh nghiệm';
            $this->description = 'Các bài viết chia sẻ bởi Sudo Team';
            if($this->page >= 2)
                $this->url = DOMAIN.'/chia-se/trang_'.$this->page;
            else 
                $this->url = DOMAIN.'/chia-se/';
        }
    }
    
    public function list_share() {  
        $page = ($this->page >= 2) ? $this->page : 1;
        $rows_per_page = 10;
        $page_start = ($page - 1) * $rows_per_page;
        $page_end = $page * $rows_per_page;
        
        if($this->id == 0) {
            $str_query_cat = '';
        }else {
            $str_query_cat = 'AND sha_cat_id = '.$this->id;
        }
        
        $query = new db_query('SELECT sha_id,sha_title,sha_cat_id,sha_summary,sha_picture,sha_date
                                FROM share
                                WHERE sha_active = 1 '.$str_query_cat.'
                                LIMIT '. $page_start . ', '. $rows_per_page .'');
        return $query->resultArray();
    }
    public function share_total_comment($share_id) {
        $query = new db_query('SELECT sta_comment FROM share_statistics WHERE sta_share_id = '.$share_id.' LIMIT 1');
        $result = mysql_fetch_assoc($query->result);
        if($result) 
            return $result['sta_comment'];
        else 
            return 0;
    }
}
?>